package com.wn.service;

import com.github.pagehelper.PageInfo;
import com.wn.common.pojo.PageParam;
import com.wn.common.pojo.ResProduct;
import com.wn.common.pojo.Rsp;
import com.wn.mapper.ResProductMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class ResProductService {
    @Autowired
    private ResProductMapper resProductMapper;

    //查找所有带分页
    public Rsp QueryAll(PageParam<ResProduct> pageParam){
        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
        List<ResProduct> resProductList = resProductMapper.queryAll();
        if(StringUtils.isEmpty(resProductList)){
           return new Rsp().msg("查询失败");
        }else {
            PageInfo pageInfo = new PageInfo(resProductList);
            return  new Rsp().success(pageInfo);
        }
    }

    //根据id查找
    public Rsp getProductById(Integer tourGuideId){
        ResProduct resProduct = resProductMapper.selectByPrimaryKey(tourGuideId);
        if(resProduct.equals(null)){
            return new Rsp().msg("查找失败");
        }else{
            return new Rsp().success(resProduct);
        }
    }

    //根据id删除(物理删除)
    public Rsp deleteByPrimairyId(Integer tourGuideId){
        int i = resProductMapper.deleteByPrimaryKey(tourGuideId);
        if(i == 1){
            return new Rsp().success(i);
        }else {
            return new Rsp().msg("删除失败！");
        }
    }

    //增加数据
    public Rsp insertAll(ResProduct resProduct){
        Integer insert = resProductMapper.insert(resProduct);
        if(StringUtils.isEmpty(insert)){
            return new Rsp().msg("添加失败！");
        }else{
            return new Rsp().success(insert);
        }
    }

    //更新数据
    public Rsp updateById(ResProduct resProduct){
        int i = resProductMapper.updateByPrimaryKeySelective(resProduct);
        if(i == 1){
            return new Rsp().success(i);
        }else {
            return new Rsp().msg("更新失败！");
        }
    }
}
